package com.xsmart.order.dao.wx;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface WxOrderListDao {

    /**
     * 查询订单详情
     * @param orderMap
     * @return
     */
    @Select("<script>" +
            "select t1.order_id, order_sn, shipping_fee, goods_amount, money_paid, " +
            "consignee, mobile, full_address, order_amount, " +
            "DATE_FORMAT(t1.add_time,'%Y-%m-%d %T') add_time, " +
            "DATE_FORMAT(t1.confirm_time,'%Y-%m-%d %T') confirm_time, " +
            "DATE_FORMAT(t1.pay_time,'%Y-%m-%d %T') pay_time, " +
            "DATE_FORMAT(t1.shipping_time,'%Y-%m-%d %T') shipping_time " +
            "from order_info t1 " +
            "left join order_shipping_info t2 on t1.order_id = t2.order_id " +
            "where t1.order_id = #{orderId}" +
            "</script>")
    Map<String, Object> quryOrderDetail(Map<String, Object> orderMap);

    /**
     * 查询订单
     * @param orderMap
     * @return
     */
    @Select("<script>" +
            "SELECT a.order_id,a.user_id,a.shipping_id,a.pay_id,a.order_sn,a.order_status,a.shipping_status, " +
            "a.pay_status,a.postscript,a.goods_amount,a.shipping_fee,a.money_paid,a.order_amount," +
            "DATE_FORMAT(a.add_time,'%Y-%m-%d %T') add_time, " +
            "DATE_FORMAT(a.confirm_time,'%Y-%m-%d %T') confirm_time, " +
            "DATE_FORMAT(a.pay_time,'%Y-%m-%d %T') pay_time, " +
            "DATE_FORMAT(a.shipping_time,'%Y-%m-%d %T') shipping_time " +
            "from order_info a " +
            "where a.user_id = #{userId} " +
            "<if test=\"orderStatus!=null\">and a.order_status=#{orderStatus}</if> " +
            "<if test=\"shippingStatus!=null\">and a.shipping_status=#{shippingStatus}</if> " +
            "<if test=\"payStatus!=null\">and a.pay_status=#{payStatus}</if> " +
            "</script> ")
    List<Map<String, Object>> queryOrderList(Map<String, Object> orderMap);

    /**
     * 查询订单商品
     * @param orderId
     * @return
     */
    @Select("<script>" +
            "SELECT b.order_id, b.goods_id,b.goods_sn,b.goods_name,b.goods_number,b.market_price,b.goods_price,b.is_gift,b.is_real, " +
            "b.cost_price, b.parent_goods_id, c.goods_img,c.is_promote,c.promote_price," +
            "DATE_FORMAT(c.promote_start_date,'%Y-%m-%d %T') promote_start_date," +
            "DATE_FORMAT(c.promote_end_date,'%Y-%m-%d %T') promote_end_date," +
            "<![CDATA[(case when (select COUNT(*) from order_goods_appraise a where a.order_id =#{orderId} and a.goods_id = b.goods_id )> 0 then 1 else 0 end)]]>  pingjia " +
            "from order_goods b " +
            "LEFT JOIN goods c on c.goods_id = b.goods_id " +
            "where b.order_id = #{orderId} " +
            "</script> ")
    List<Map<String, Object>> queryOrderGoodsList(Integer orderId);

    /**
     * 取消订单查询订单商品信息
     * @param orderId
     * @return
     */
    @Select("<script>" +
            "SELECT b.goods_id, b.goods_number " +
            "from order_goods b " +
            "LEFT JOIN goods c on c.goods_id = b.goods_id " +
            "where b.order_id = #{orderId} " +
            "</script> ")
    List<Map<String, Object>> queryOrderGoodsNumber(Integer orderId);

    /**
     * 根据订单的三种状态查询订单的数量
     * @param userId
     * @param orderStatus
     * @param shippingStatus
     * @param payStatus
     * @return
     */
    @Select("select count(1) from order_info a " +
            "where a.user_id = #{userId} " +
            "and a.order_status=#{orderStatus} " +
            "and a.shipping_status=#{shippingStatus} " +
            "and a.pay_status = #{payStatus} ")
    @ResultType(Integer.class)
    int queryOrderCountByStatus(@Param("userId") Integer userId,@Param("orderStatus") Integer orderStatus,
                        @Param("shippingStatus") Integer shippingStatus,@Param("payStatus") Integer payStatus);

    /**
     * 获取订单的状态
     * @param orderId
     * @return
     */
    @Select("<script>select order_status*1 as order_status, shipping_status*1 as shipping_status, pay_status*1 as pay_status, pay_id, pay_time * 1 as pay_time from order_info where order_id=#{orderId}</script>")
    Map<String,Object> getOrderStatus(Integer orderId);

    /**
     *修改订单的状态
     * @param orderStatus
     * @return
     */
    @Update("<script>update order_info set order_status=#{order_status}, " +
            "shipping_status=#{shipping_status}," +
            "pay_status=#{pay_status}," +
            "pay_id=#{pay_id}," +
            "pay_time=#{pay_time} "+
            "where order_id=#{order_id}</script>")
    int modOrderStatus(Map<String,Object> orderStatus);

    /**
     * 查询分销商的订单
     * @param orderMap
     * @return
     */
    @Select("select t.order_id,t.user_id,t.shipping_id,t.pay_id,t.order_sn,t.order_status,t.shipping_status," +
            "t.pay_status,t.postscript,t.goods_amount,t.shipping_fee,t.money_paid,t.order_amount,t.add_time," +
            "t.confirm_time,t.pay_time,t.shipping_time,tt.nick_name " +
            "from order_info t " +
            "left JOIN INFS_USER tt on tt.user_id = t.user_id " +
            "where EXISTS(" +
            "SELECT t1.id from seller_shopinfo t1 " +
            "where t1.ru_id=#{userId} and t1.id = t.seller_id ) " +
            "and (SELECT t2.user_type from infs_user t2 where t2.user_id = #{userId}) = 1 " +
            "and t.order_status = #{orderStatus} and t.shipping_status = #{shippingStatus} and t.pay_status = #{payStatus}")
    List<Map<String, Object>> queryDistributerOrder(Map<String, Object> orderMap);

    /**
     * 查询分销商的订单数量
     * @param userId
     * @return
     */
    @Select("select count(1) from order_info t " +
            "left JOIN INFS_USER tt on tt.user_id = t.user_id " +
            "where EXISTS(" +
            "SELECT t1.id from seller_shopinfo t1 " +
            "where t1.ru_id=#{userId} and t1.id = t.seller_id ) " +
            "and (SELECT t2.user_type from infs_user t2 where t2.user_id = #{userId}) = 1")
    @ResultType(Integer.class)
    int queryDistributerOrderCount(@Param("userId") Integer userId);

    /**
     * 查询分销商的下线会员信息
     * @param userId
     * @return
     */
    @Select("select b.nick_name,b.user_picture from infu_distributor_member a  " +
            "LEFT JOIN infs_user b on a.user_id = b.user_id " +
            "where a.d_user_id=#{userId}")
    List<Map<String,Object>> queryDistributorMember(@Param("userId") Integer userId);

    /**
     * 新增订单结算记录
     * @param orderSettleMap
     * @return
     */
    @Insert("insert into order_settle(order_id, cost_price, goods_amount, order_profit) " +
            "values(#{order_id}, #{cost_price}, #{goods_amount}, #{order_profit})")
    int addOrderSettle(Map<String, Object> orderSettleMap);

    /**
     * 查询是否存在该条订单结算记录
     * @param orderId
     * @return
     */
    @Select("select * from order_settle where order_id=#{orderId}")
    List<Map<String, Object>> queryOrderSettle(@Param("orderId")Integer orderId);
}
